﻿using BaseDll;
using CommonTools;
using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace StationDemo.FrameUI
{
    public partial class Form_UserManager : UIPage
    {
        public Form_UserManager()
        {
            InitializeComponent();
        }
        public UserRight userRight
        {
            get;
            set;
        }

        public void ChangedUserRight(User CurrentUser)
        {

            if (userRight == CurrentUser._userRight)
                return;
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => ChangedUserRight(CurrentUser)));
            }
            else
            {
                userRight = CurrentUser._userRight;
                comboBox_SelRight.Items.Clear();
                for (int i = 0; i <= (int)CurrentUser._userRight; i++)
                {
                    comboBox_SelRight.Items.Add(((UserRight)i).ToString());
                }
                comboBox_SelRight.SelectedIndex = 0;
                dataGridView_UserList.Rows.Clear();
                foreach (var temp in sys.g_listUser)
                {
                    if ((int)temp._userRight <= (int)userRight)
                    {
                        dataGridView_UserList.Rows.Add(temp._userName,
                          temp._userRight.ToString());
                    }
                }

                if ((int)CurrentUser._userRight < (int)UserRight.Debug)
                {
                    for (int i = 0; i < panUP.Controls.Count; i++)
                        panUP.Controls[i].Enabled = false;
                    button_DelUser.Enabled = false;
                    uiSymbolModifyPassWord.Enabled = false;

                }
                else
                {
                    for (int i = 0; i < panUP.Controls.Count; i++)
                        panUP.Controls[i].Enabled = true;
                    button_DelUser.Enabled = true;
                    uiSymbolModifyPassWord.Enabled = true;
                }
                dataGridView_UserList.ForeColor = Color.Black;
                //int heith = 0;
                //for (int i = 0; i < dataGridView_UserList.Rows.Count; i++)
                //    heith +=  dataGridView_UserList.Rows[i].Height;
                //heith += dataGridView_UserList.ColumnHeadersHeight;
                //dataGridView_UserList.Height = heith+30;
            }
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            if (textBox_UserName.Text == "" || textBox_PassWord.Text == "")
            {
                UIMessageBox.ShowWarning("用户名和密码不能为空 ");
                return;
            }
            string str = ((UserRight)comboBox_SelRight.SelectedIndex).ToString();
            int userIndex = sys.g_listUser.FindIndex(t => t._userName == textBox_UserName.Text);
            if (userIndex != -1)
            {
                UIMessageBox.ShowWarning("用户名已存在 ");
                return;

            }

            dataGridView_UserList.Rows.Add(textBox_UserName.Text, str);
            sys.g_listUser.Add(new User()
            {
                _userName = textBox_UserName.Text,
                _userPassWord = textBox_PassWord.Text,
                _userRight = (UserRight)comboBox_SelRight.SelectedIndex
            });
            ConfigToolMgr.GetInstance().SaveUserConfig();
        }

        private void button_DelUser_Click(object sender, EventArgs e)
        {
            if (dataGridView_UserList.SelectedRows.Count > 0)
            {
                /*    DataGridViewSelectedRowCollection r0 =*/
                bool bDel = false;
                string str = dataGridView_UserList.SelectedRows[0].Cells[0].Value.ToString();
                User delobj = new User();
                foreach (var temp in sys.g_listUser)
                {
                    if (temp._userName == str)
                    {
                        delobj = temp;
                        bDel = true;
                    }

                }
                DataGridViewRow row = dataGridView_UserList.SelectedRows[0];
                dataGridView_UserList.Rows.Remove(row);
                if (bDel)
                {
                    sys.g_listUser.Remove(delobj);
                    ConfigToolMgr.GetInstance().SaveUserConfig();
                }

            }
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            userRight = UserRight.User_None;
            sys.g_eventRightChanged += ChangedUserRight;
        }

        private void uiSymbolModifyPassWord_Click(object sender, EventArgs e)
        {
            string strOldPass = "";
            User user = new User();
            user = sys.g_User;
            if (dataGridView_UserList.CurrentCell != null)
            {
                int nIndexRow = dataGridView_UserList.CurrentCell.RowIndex;
                int nIndex = sys.g_listUser.FindIndex(t => t._userName == dataGridView_UserList.Rows[nIndexRow].Cells[0].Value.ToString());
                if (-1 != nIndex)
                {
                    user = sys.g_listUser[nIndex];

                }
            }
            if (UIInputDialog.InputPasswordDialog(this, ref strOldPass, true, $"请用户: {user._userName}  输入原密码"))
            {
                if (user._userPassWord == strOldPass)
                {
                    if (UIInputDialog.InputPasswordDialog(this, ref strOldPass, true, $"请用户: {user._userName}  输入新密码"))
                    {
                        int nIndex = sys.g_listUser.FindIndex(t => t._userName == user._userName);
                        if (-1 != nIndex)
                        {
                            sys.g_listUser[nIndex] = new User()
                            {
                                _userPassWord = strOldPass,
                                _userName = user._userName,
                                _userRight = user._userRight,
                            };

                            ConfigToolMgr.GetInstance().SaveUserConfig();
                            this.ShowInfoNotifier($"用户 : {user._userName} 密码被修改");
                        }
                    }
                    else
                        UIMessageBox.ShowError("密码输入错误");
                }
                else
                    UIMessageBox.ShowError("原密码输入错误");
            }

        }
    }
}
